<template>
  <div class="scalable-pages">
    <main class="main">
      <div class="container">
        <h2 class="product-title">KDX漆面保护膜</h2>

        <!-- 产品系列导航 -->
        <div class="product-series">
          <div class="series-container">
            <div class="arrow-left" @click="scrollSeries(-1)">
              <i class="iconfont icon-arrowleft"></i>
            </div>
            <ul class="series-list" ref="seriesList">
              <li v-for="(item, index) in visibleSeries" :key="index"
                :class="{ active: selectedIndex === item.originalIndex }" @click="handleNavClick(item.originalIndex)">
                <span class="text">{{ item.name }}</span>
                <div class="cover"></div>
                <div class="bg"></div>
              </li>
            </ul>
            <div class="arrow-right" @click="scrollSeries(1)">
              <i class="iconfont icon-arrowright"></i>
            </div>
          </div>
        </div>

        <!-- 产品详情 -->
        <div class="product-detail" v-if="filmInfo">
          <div class="detail-header">
            <div class="second-nav-container">
              <div class="arrow-left" @click="scrollProducts(-1)">
                <i class="iconfont icon-arrow-left"></i>
              </div>
              <div class="second-nav-tab">
                <div v-for="(item, index) in visibleProducts" :key="index" class="item" :class="{
                  active: selectedSecondIndex === item.originalIndex,
                }" @click="handleNavClick2(item.originalIndex)">
                  {{ item.name }}
                </div>
              </div>
              <div class="arrow-right" @click="scrollProducts(1)">
                <i class="iconfont icon-arrow-right"></i>
              </div>
            </div>
            <div class="specs">
              <span class="mil">
                <img src="https://www.kdxwindowfilm.com/banners/static/houdu.png" alt="" />
                {{ filmInfo.thickness }}mil</span>
              <span class="warranty">
                <img src="https://www.kdxwindowfilm.com/banners/static/zhibao.png" alt="" />
                {{ filmInfo.warranty }}年</span>
            </div>
          </div>

          <div class="desc">{{ filmInfo.description }}</div>

          <div class="flex flex-row product-detail-inner">
            <!-- 评分区域 -->
            <div class="rating-section">
              <div class="rating-item">
                <span class="label spec-label">{{ filmInfo.is_yg === '1' ? "哑光" : "亮度" }}指数：</span>
                <div class="stars">
                  <rate :modelValue="filmInfo.brightness_enhancement"></rate>
                </div>
              </div>
              <div class="rating-item">
                <span class="label spec-label">防刮性能：</span>
                <div class="stars">
                  <rate :modelValue="filmInfo.scratch_resistance"></rate>
                </div>
              </div>
              <div class="rating-item">
                <span class="label spec-label">抗污指数：</span>
                <div class="stars">
                  <rate :modelValue="filmInfo.stain_resistance"></rate>
                </div>
              </div>
              <div class="rating-item">
                <span class="label spec-label">耐用指数：</span>
                <div class="stars">
                  <rate :modelValue="filmInfo.durability_index"></rate>
                </div>
              </div>
            </div>

            <div class="product-image">
              <img :src="filmInfo.image" :alt="filmInfo.name" v-animate-on-scroll="'slideInRight'" />
            </div>
          </div>
        </div>
      </div>
    </main>
  </div>
</template>

<script>
import { get } from "@/utils/request";
import rate from "../components/rate.vue";

export default {
  components: {
    rate,
  },
  data() {
    return {
      selectedIndex: 0,
      selectedSecondIndex: 0,
      navList: [],
      secondNavList: [],
      filmInfo: null,
      seriesStartIndex: 0,
      productsStartIndex: 0,
      visibleSeriesCount: 3, // 分类列表显示数量
      visibleProductsCount: 4, // 产品列表显示数量
    };
  },
  computed: {
    visibleSeries() {
      return this.navList
        .slice(
          this.seriesStartIndex,
          this.seriesStartIndex + this.visibleSeriesCount
        )
        .map((item, index) => ({
          ...item,
          originalIndex: this.seriesStartIndex + index,
        }));
    },
    visibleProducts() {
      return this.secondNavList
        .slice(
          this.productsStartIndex,
          this.productsStartIndex + this.visibleProductsCount
        )
        .map((item, index) => ({
          ...item,
          originalIndex: this.productsStartIndex + index,
        }));
    },
  },
  methods: {
    handleNavClick(index) {
      this.selectedIndex = index;
      this.getFilmList();
      this.selectedSecondIndex = 0;
      this.productsStartIndex = 0; // 切换分类时重置产品列表起始位置
    },
    handleNavClick2(index) {
      this.selectedSecondIndex = index;
      this.filmInfo = this.secondNavList[index];
    },
    scrollSeries(direction) {
      const newIndex = this.seriesStartIndex + direction;
      if (
        newIndex >= 0 &&
        newIndex <= this.navList.length - this.visibleSeriesCount
      ) {
        this.seriesStartIndex = newIndex;
      }
    },
    scrollProducts(direction) {
      const newIndex = this.productsStartIndex + direction;
      if (
        newIndex >= 0 &&
        newIndex <= this.secondNavList.length - this.visibleProductsCount
      ) {
        this.productsStartIndex = newIndex;
      }
    },
    getNavList() {
      get("/filmCategory/list", { type: "paint" }).then((res) => {
        if (res.code == 200) {
          this.navList = res?.data || [];
          if (this.navList.length > 0) {
            this.getFilmList();
          }
        }
      });
    },
    getFilmList() {
      get("/paintFilm/list", {
        pageSize: 100,
        category_id: this.navList[this.selectedIndex].id,
      }).then((res) => {
        if (res.code == 200) {
          this.secondNavList = res?.data?.list || [];
          if (this.secondNavList.length > 0) {
            this.filmInfo = this.secondNavList[this.selectedSecondIndex];
          }
        }
      });
    },
  },
  mounted() {
    this.getNavList();
  },
};
</script>

<style lang="less" scoped>
/* 原有样式保持不变 */
.spec-label {
  color: rgb(11, 11, 11) !important;
  font-size: 30px;
}

.banners {
  width: 100%;
  display: black;
}

.swipers {
  width: 100%;
  /* height: 500px; */
}

.main {
  background-color: white;
}

.container {
  width: 1567px;
  margin: 0 auto;
  padding: 100px 0;
}

.product-title {
  font-size: 48px;
  margin-bottom: 30px;
  color: #333;
  text-align: center;
  margin-bottom: 71px;
  font-weight: bold;
}

.product-series {
  margin-bottom: 40px;
  position: relative;

  .series-container {
    position: relative;
    width: 100%;
  }

  .arrow-left,
  .arrow-right {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    cursor: pointer;
    font-size: 24px;
    padding: 10px;
    color: #333;
    transition: all 0.3s;
    z-index: 10;
    background: rgba(255, 255, 255, 0.7);
    border-radius: 50%;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);

    &:hover {
      color: #f5000c;
      background: rgba(255, 255, 255, 0.9);
    }
  }

  .arrow-left {
    left: -10px;
  }

  .arrow-right {
    right: -10px;
  }

  .series-list {
    display: flex;
    justify-content: center;
    list-style: none;
    padding: 0;
    margin: 0 auto;
    width: calc(100% - 80px);

    li {
      position: relative;
      margin: 0 1.1vw;
      padding: 2.6vw 0;
      cursor: pointer;
      transition: all 0.3s ease;
      display: flex;
      flex: 1;
      background-color: #333;
      box-sizing: border-box;
      align-items: center;
      justify-content: center;
      border-radius: 4px;
      overflow: hidden;
      height: 115px;
      transition: all 0.3s ease;
      min-width: 300px;

      &.active {
        .cover {
          background-color: #f5000c;
        }
      }

      .text {
        color: #fff;
        font-size: 1.8vw;
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        z-index: 5;
        user-select: none;
      }

      .bg {
        background-image: url("https://www.kdxwindowfilm.com/banners/static/3d0753b37cab53d025e7e34c7524956a87c84f024462f-bY5h99_fw1200.png");
        background-size: 73%;
        background-position: center center;
        background-repeat: no-repeat;
        position: absolute;
        filter: grayscale(100%);
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 1;
      }

      .cover {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background-color: #555;
        opacity: 0.7;
        z-index: 2;
      }
    }
  }
}

.product-detail {
  padding: 30px;
  border-radius: 8px;

  .desc {
    font-size: 28px;
    margin-bottom: 103px;
    margin-top: 50px;
  }
}

.detail-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
  padding-bottom: 15px;
}

.second-nav-container {
  position: relative;

  .arrow-left,
  .arrow-right {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    cursor: pointer;
    font-size: 20px;
    padding: 5px;
    color: #333;
    transition: all 0.3s;
    z-index: 10;
    background: rgba(255, 255, 255, 0.7);
    border-radius: 50%;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);

    &:hover {
      color: #f5000c;
      background: rgba(255, 255, 255, 0.9);
    }
  }

  .arrow-left {
    left: -55px;
  }

  .arrow-right {
    right: -35px;
  }
}

.second-nav-tab {
  display: flex;
  justify-content: center;
  list-style: none;
  padding: 0;
  margin: 0 auto;

  .item {
    padding: 10px 18px;
    cursor: pointer;
    transition: all 0.3s ease;
    margin-right: 1.3vw;
    font-size: 28px;
    background-color: #f5f5f5;
    box-sizing: border-box;
    font-size: min(32px, 32px);
    min-width: 150px;
    text-align: center;

    &.active {
      background-color: #fff;
      border: 0.5px solid #ccc;
      border-radius: 4px;
    }
  }
}

.detail-title {
  font-size: 24px;
  color: #333;
  margin: 0;
}

.specs {
  display: flex;
  gap: 20px;
}

.mil,
.warranty {
  display: inline-flex;
  align-items: center;
  padding: 5px 15px;
  font-size: 40px;
  font-weight: bold;
  color: #595758;

  img {
    width: 80px;
    height: 70px;
    margin-right: 10px;
  }
}

.product-detail-inner {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.rating-section {
  margin-bottom: 30px;
  width: 40%;

  .rating-item {
    display: flex;
    align-items: center;
    margin-bottom: 1.2vw;

    .stars {
      margin-left: 20px;
    }
  }
}

.product-image {
  display: flex;
  flex: 1;

  img {
    width: 100%;
    height: auto;
  }
}
</style>